package com.hy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hy.common.PageResult;
import com.hy.common.QueryPageBean;
import com.hy.common.Result;
import com.hy.file.service.FileStorageService;
import com.hy.mapper.DoctorsBigDepMapper;
import com.hy.mapper.DoctorsMapper;
import com.hy.mapper.DoctorsHospitalMapper;
import com.hy.mapper.PatientInfoMapper;
import com.hy.pojo.*;
import com.hy.service.DoctorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class DoctorsServiceImpl implements DoctorsService {
    @Autowired
    private DoctorsMapper doctorsMapper;
    @Autowired
    private DoctorsHospitalMapper hospitalMapper;

    @Autowired
    private DoctorsBigDepMapper depMapper;
    @Autowired
    private FileStorageService fileStorageService;
    @Autowired
    private PatientInfoMapper patientInfoMapper;
    @Override
    public PageResult getDoctor(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Doctors> doctorsList=doctorsMapper.getDoctor(queryPageBean.getQueryString());
        PageInfo<Doctors>pageInfo=new PageInfo<>(doctorsList);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public Result addDoctor(Doctors doctors) {
        doctorsMapper.addDoctor(doctors);
        DoctorsHospital hospital=hospitalMapper.getHospitals(doctors.getHospitalId());
        hospital.setDoctorsNum(hospital.getDoctorsNum()+1);
        hospitalMapper.updateHospital(hospital);
        DoctorsBigDep bigdep=depMapper.getDepById(doctors.getDepartmentId());
        bigdep.setDoctorsNum(bigdep.getDoctorsNum()+1);
        depMapper.updateDep(bigdep);
        return new Result(true,"添加成功");
    }

    @Override
    public Doctors doctorXx(Integer id)  {
       return doctorsMapper.getDoctorXx(id);
    }

    @Override
    public Result getBigdep() {
        List<DoctorsBigDep>bigdepList=depMapper.getBigdep();
        return new Result(true,"获取成功",bigdepList);

    }

    @Override
    public Result getHospition() {
        List<DoctorsHospital>hospitalList=hospitalMapper.getHospition();
        return new Result(true,"获取成功",hospitalList);
    }

    @Override
    public Result updateDoctors(Doctors doctors) {
        doctorsMapper.updateDoctors(doctors);
        return new Result(true,"修改成功");
    }

    @Override
    public String updatefile(MultipartFile file)throws Exception {
        if(file==null || file.getSize()<0){
            return "";
        }
        String s = fileStorageService.uploadImageFile(file.getOriginalFilename(), file.getInputStream());
        return s;
    }

    @Override
    public Result patientBydoctorId(Integer doctorId) {
       List<PatientInfo>patientInfo=patientInfoMapper.getPatientByDoctorId(doctorId);
        return new Result(true,"查询成功",patientInfo);
    }

    @Override
    public Result delDoctors(Integer id) {
        Doctors doctors=doctorsMapper.getDoctorXxByid(id);
        DoctorsHospital hospital=hospitalMapper.getHospitals(doctors.getHospitalId());
        System.err.println(hospital);
        hospital.setDoctorsNum(hospital.getDoctorsNum()-1);
        hospitalMapper.updateHospital(hospital);
        DoctorsBigDep bigdep=depMapper.getDepById(doctors.getDepartmentId());
        bigdep.setDoctorsNum(bigdep.getDoctorsNum()-1);
        depMapper.updateDep(bigdep);
        doctorsMapper.delDoctors(id);
        return new Result(true,"删除成功");
    }

    @Override
    public Result PatienevaluationBydoctorId(Integer doctorId) {
        List<Patienevaluation> patienevaluationList=doctorsMapper.PatienevaluationBydoctorId(doctorId);
        return new Result(true,"查询成功",patienevaluationList);
    }
}
